﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using WindowsPhoneRssReader.Models;

namespace WindowsPhoneRssReader
{
    public partial class FeedDetailsPage
    {
        private Feed _viewModel;
        public FeedDetailsPage()
        {
            InitializeComponent();
            PageTransitionDetails.Completed += PageTransitionDetails_Completed;
            Loaded += FeedDetailsPage_Loaded;
            PageTransitionList.Completed += PageTransitionList_Completed;

            var root = Application.Current.RootVisual as FrameworkElement;
            if (root == null) return;
            var currentFeed = root.DataContext as Feed;
            _viewModel = currentFeed;
        }

        void PageTransitionList_Completed(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/FeedItemDetailsPage.xaml", UriKind.Relative));
            var root = Application.Current.RootVisual as FrameworkElement;
            if (root == null) return;
            root.DataContext = _viewModel.CurrentFeedItem;

        }

        private void lstFeeds_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listBox = sender as ListBox;
            if (listBox == null) return;
            var selected = listBox.SelectedItem as FeedItem;
            if (selected == null) return;
            _viewModel.CurrentFeedItem = selected;
            PageTransitionList.Begin();
        }
        void FeedDetailsPage_Loaded(object sender, RoutedEventArgs e)
        {
            ResetPageTransitionList.Begin();
            DataContext = _viewModel;
        }

        void PageTransitionDetails_Completed(object sender, EventArgs e)
        {
            var root = (PhoneApplicationFrame)Application.Current.RootVisual;
            if (root == null) return;
            root.GoBack();
        }



        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            PageTransitionDetails.Begin();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            lstFeeds.SelectedIndex = -1;
        }

        private void MarkAllAsRead(object sender, EventArgs e)
        {
            var model = DataContext as Feed;
            if (model == null) return;
            foreach (var item in model.FeedItems.Where(item => !item.Read))
            {
                item.Read = true;
            }
        }

        private void MarkAllAsUnRead(object sender, EventArgs e)
        {
            var model = DataContext as Feed;
            if (model == null) return;
            foreach (var item in model.FeedItems.Where(item => item.Read))
            {
                item.Read = false;
            }
        }

    }
}